package router

import (
	platformController "gitee.com/wangshh_123/cross-pay-go/src/controller/platform"
	"gitee.com/wangshh_123/cross-pay-go/src/middleware"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/integration"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/hlPay"
	"github.com/gin-gonic/gin"
)

// SetupHlPayRouter 设置HlPay路由
func SetupHlPayRouter(api *gin.RouterGroup, hlPayClient *hlPay.HlPayClient) {
	// 创建HlPay控制器实例
	hlPayController := platformController.NewHlPayController(hlPayClient)
	hlPayAccountBalanceController := platformController.NewHlPayAccountBalanceController()

	// 创建统一交易控制器实例（用于批量开卡）
	platformManager := integration.NewPlatformManagerWithClients(nil, hlPayClient)
	unifiedTransactionController := platformController.NewUnifiedTransactionController(platformManager)

	// 创建统一卡片控制器实例（用于统一提现）
	unifiedCardController := platformController.NewUnifiedCardController(nil, hlPayClient)

	// HlPay路由组
	hlpay := api.Group("/hlpay")
	{
		// 卡片相关路由
		cards := hlpay.Group("/cards")
		cards.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			// 卡片查询相关
			cards.GET("/bin", hlPayController.GetCardBin)                                                                                                             // 获取卡Bin信息
			cards.GET("/state", hlPayController.GetStateList)                                                                                                         // 获取州列表
			cards.POST("/list", hlPayController.GetCardList)                                                                                                          // 获取卡片列表
			cards.GET("/task/:taskId", hlPayController.GetCardTask)                                                                                                   // 查询开卡任务
			cards.GET("/detail/:id", hlPayController.GetCardDetail)                                                                                                   // 获取卡片详情
			cards.GET("/verification/code/:id", hlPayController.GetVerificationCode)                                                                                  // 获取卡片验证码
			cards.GET("/history", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), platformController.NewCardHistoryController().GetCardHistoryList) // 获取卡历史列表

			// 卡片操作相关
			cards.POST("/open", hlPayController.OpenCard)                                                                                           // 开卡
			cards.POST("/batch-open", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedTransactionController.BatchOpenCard) // 批量开卡 - 使用统一接口
			// cards.POST("/update", hlPayController.UpdateCard)         // 更新卡片
			cards.PUT("/freeze/:id", hlPayController.FreezeCard)    // 冻结卡片
			cards.PUT("/enable/:id", hlPayController.EnableCard)    // 解冻卡片
			cards.DELETE("/cancel/:id", hlPayController.CancelCard) // 注销卡片

			// 卡片充值相关
			cards.PUT("/recharge", hlPayController.Recharge)                                                                              // 充值
			cards.PUT("/withdraw", hlPayController.Withdraw)                                                                              // 提现
			cards.POST("/withdraw", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.WithdrawCard) // 统一提现 - 使用统一接口
			cards.POST("/recharge-return", hlPayController.RechargeReturn)                                                                // 金额退还
		}

		// 交易相关路由
		transactions := hlpay.Group("/transactions")
		transactions.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			transactions.GET("/list", hlPayController.GetTransactionList) // 获取交易列表
			// transactions.GET("/detail", hlPayController.GetTransactionDetail) // 获取交易详情
		}

		// 账户余额相关路由
		balances := hlpay.Group("/balances")
		balances.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			balances.GET("/", hlPayAccountBalanceController.GetAccountBalances)                     // 获取账户余额列表
			balances.GET("/:accountNo", hlPayAccountBalanceController.GetAccountBalanceByAccountNo) // 根据账户号获取账户余额
			balances.GET("/total", hlPayAccountBalanceController.GetTotalBalance)                   // 获取总余额
			balances.GET("/debug", hlPayAccountBalanceController.DebugPlatformInfo)                 // 调试平台信息（临时）
		}

		// 测试路由（不需要平台信息中间件）
		test := hlpay.Group("/test")
		test.Use(middleware.AuthMiddleware())
		{
			test.GET("/simple", hlPayAccountBalanceController.TestSimpleEndpoint) // 简单测试接口
		}
	}
}
